//
//  extUart.h
//  NMEA
//
//  Created by Hans Johnson on 2/6/2014.
//
//

#ifndef NMEA_extUart_h
#define NMEA_extUart_h

#include "ebi.h"

/* BaudRate Divisors assuming a 14.7456 MHz clock*/
#define B1200 0
#define B2400 1
#define B4800 2
#define B9600 3
#define B19200 4
#define B38400 5
#define B57600 6

/* Serial Block Length */
#define BLOCK_LENGTH 32


#ifndef __FLASH
#warning Memory Spaces Not Enabled
#endif




/* Structure for the UART register set */

typedef struct UART_REG_struct
{
    register8_t UART_RHR_THR; /* Receive/Transmit Holding Register */
    register8_t UART_IER; /* Interrupt Enable Register */
    register8_t UART_IIR_FCR; /* Interrupt Identification Register/Fifo Control Register */
    register8_t UART_LCR; /* Line Control Register */
    register8_t UART_MCR; /* Modem Control Register */
    register8_t UART_LSR; /* Line Status Register */
    register8_t UART_MSR; /* Modem Status Register */
    register8_t UART_SPR; /* Scratchpad Register */
} UART_REG_t;

/* Define the External UART Base Addresses */

/* Now create the register map... These are in memory */
#define EXTUART0 ( *(UART_REG_t *) UART_BASE_ADDR) /* UART 0 */
#define EXTUART1 ( *(UART_REG_t *) (UART_BASE_ADDR + 0x0008)) /* UART 1 */
#define EXTUART2 ( *(UART_REG_t *) (UART_BASE_ADDR + 0x0010))
#define EXTUART3 ( *(UART_REG_t *) (UART_BASE_ADDR + 0x0018))
#define EXTUART4 ( *(UART_REG_t *) (UART_BASE_ADDR + 0x0020))
#define EXTUART5 ( *(UART_REG_t *) (UART_BASE_ADDR + 0x0028))
#define EXTUART6 ( *(UART_REG_t *) (UART_BASE_ADDR + 0x0030))
#define EXTUART7 ( *(UART_REG_t *) (UART_BASE_ADDR + 0x0038))

/* Function to setup UART */
void uartSetBaud (void *Uart, uint8_t BaudDivisor);
void uartSet8N1 (void *extUart);
void uartInit (void *extUart);
void extUartTxTLR (UART_REG_t *extUart, uint8_t level);
int8_t sendChar (void *Uart, char toSend, portTickType timeout);
int8_t readChar(void *Uart, char *toRead, portTickType timeout);
int8_t sendLine (void *Uart, char *toSend, uint8_t txLength);
int8_t readLine (void *Uart, char *toRead, uint8_t rxLength, char termChar);
#endif

